import cn.lizemin.toExcel.Student;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.junit.Test;

import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @author: lizemin
 * @version: 1.0
 */
public class ExportExcel {

    public static <N> List getList(){
        return new ArrayList<N>();

    }

    @Test
    public void test1(){
        //获取一个集合中对象的属性的属性值
        List list = getList();
        list.add(new Student("lizemin",28));

        //获取这个集合对象的属性
        Field[] declaredFields = list.get(0).getClass().getDeclaredFields();
        for (Field declaredField : declaredFields) {
            System.out.println(declaredField.getName());
        }

    }


    /*public static int exportToExcel(List objData, String sheetName,
                                    List<String> columns) {
        int flag = 0;
        //声明工作簿jxl.write.WritableWorkbook
        WritableWorkbook lizemin;

        try {
            //根据传进来的file对象创建可写入的Excel工作薄
            OutputStream os = response.getOutputStream();

            lizemin = jxl.Workbook.createWorkbook(os);

            *//*
             * 创建一个工作表、sheetName为工作表的名称、"0"为第一个工作表
             * 打开Excel的时候会看到左下角默认有3个sheet、"sheet1、sheet2、sheet3"这样
             * 代码中的"0"就是sheet1、其它的一一对应。
             * createSheet(sheetName, 0)一个是工作表的名称，另一个是工作表在工作薄中的位置
             *//*
            WritableSheet ws = lizemin.createSheet(sheetName, 0);

            SheetSettings ss = ws.getSettings();
            ss.setVerticalFreeze(1);//冻结表头

            WritableFont font1 = new WritableFont(WritableFont.createFont("微软雅黑"), 10, WritableFont.BOLD);
            // WritableFont font2 =new WritableFont(WritableFont.createFont("微软雅黑"), 9 ,WritableFont.NO_BOLD);
            WritableCellFormat wcf = new WritableCellFormat(font1);
            // WritableCellFormat wcf2 = new WritableCellFormat(font2);
            // WritableCellFormat wcf3 = new WritableCellFormat(font2);//设置样式，字体

            //创建单元格样式
            //WritableCellFormat wcf = new WritableCellFormat();

            //背景颜色
            // wcf.setBackground(jxl.format.Colour.YELLOW);
//            wcf.setAlignment(Alignment.CENTRE);  //平行居中
//            wcf.setVerticalAlignment(VerticalAlignment.CENTRE);  //垂直居中
            //  wcf3.setAlignment(Alignment.CENTRE);  //平行居中
            //  wcf3.setVerticalAlignment(VerticalAlignment.CENTRE);  //垂直居中
            //  wcf3.setBackground(Colour.LIGHT_ORANGE);
            // wcf2.setAlignment(Alignment.CENTRE);  //平行居中
            // wcf2.setVerticalAlignment(VerticalAlignment.CENTRE);  //垂直居中

            *//*
             * 这个是单元格内容居中显示
             * 还有很多很多样式
             *//*
            //   wcf.setAlignment(Alignment.CENTRE);
            //判断一下表头数组是否有数据
            if (columns != null && columns.size() > 0) {

                //循环写入表头
                for (int i = 0; i < columns.size(); i++) {
                    ws.setColumnView(i, 28);//设置列宽
                    ws.addCell(new Label(i, 0, columns.get(i), wcf));
                }

                //判断表中是否有数据
                if (objData != null && objData.size() > 0) {
                    //循环写入表中数据
                    for (int i = 0; i < objData.size(); i++) {


                    }
                } else {
                    flag = -1;
                }

                //写入Exel工作表
                lizemin.write();

                //关闭Excel工作薄对象
                lizemin.close();

                //关闭流
                os.flush();
                os.close();
            }
        } catch (IllegalStateException e) {
            System.err.println(e.getMessage());
        } catch (Exception ex) {
            flag = 0;
            ex.printStackTrace();
        }

        return flag;
    }*/

    @Test
    public void test8() throws IOException {
        HashMap<String, Object> hm = new HashMap<>();
        ArrayList<Integer> list1 = new ArrayList<>();
        list1.add(12);
        list1.add(13);
        list1.add(14);
        list1.add(15);

        ArrayList<Integer> list2 = new ArrayList<>();
        list2.add(22);
        list2.add(23);
        list2.add(24);
        list2.add(25);

        hm.put("服务器名称",list1);
        hm.put("服务器id",list2);

        HSSFWorkbook wb = new HSSFWorkbook();
        // Create an HSSFSheet object
        LocalDate now = LocalDate.now();
        DateTimeFormatter df = DateTimeFormatter.ISO_LOCAL_DATE;
        String str_date = df.format(now);
        HSSFSheet sheet = wb.createSheet(str_date);

        String[] column_values = new String[10];
        Set<String> keySet = hm.keySet();

        Iterator<String> it = keySet.iterator();
        int index = 0;
        while (it.hasNext()){
            column_values[index++] = it.next();
        }

        //数组下标
        /*int index = 0;
        for (String s : keySet) {
            column_values[index] = s;
            index++;
//            System.out.println(s);
        }*/


        HSSFRow filed_row = sheet.createRow(0);

        int column_count = column_values.length;

        //打印字段名
        for (int i = 0; i < column_count; i++) {
            filed_row.createCell(i).setCellValue(column_values[i]);
        }

        //将值给打印出来
        List values = (List) hm.get(column_values[0]);

        //记录数
        int row_count = values.size();

        for (int j = 0; j < column_count; j++) {    //多列
            for (int i = 0; i < row_count; i++) {
                values = (List) hm.get(column_values[0]);
                //第一行
                sheet.createRow(i + 1).createCell(j).setCellValue(String.valueOf(values.get(i)));

            }
        }

        //打印第一列

        //打印第二列
        /*for (int i = 0; i < row_count; i++) {
            values = (List) hm.get(column_values[0]);
            //第一行
            sheet.createRow(i + 1).createCell(1).setCellValue(String.valueOf(values.get(i + 1)));

        }*/


        //第二行
//        sheet.createRow(2).createCell(0).setCellValue(String.valueOf(values.get(2)));

        //第n行


        /*Iterator<String> it = keySet.iterator();
        int i = 0;
        int j = 0;
        HSSFRow filed_column = sheet.createRow(0);
        while (it.hasNext()) {
            String column_name = it.next();
            System.out.println("获取字段：" + column_name);
            filed_column.createCell(j++).setCellValue(column_name);

            //打印值
            List list = (List) hm.get(column_name);
            System.out.println("每个字段下的值：" + list);
            //将值打印到一列中
            //1 0   1
            //2 0   2
            //3 0   3
            int column_num = keySet.size();
            int value_count = list.size();
            for (int h = 0; h < column_num; h++) {
                //第一列
                for (int k = 0; k < value_count; k++) {
                    sheet.createRow(i + 1).createCell(h).setCellValue(String.valueOf(list.get(k)));

                }
            }
        }*/

            LocalDateTime nowDateTime = LocalDateTime.now();

            DateTimeFormatter df2 = DateTimeFormatter.ISO_LOCAL_DATE_TIME;

            String str_nowDateTime = df2.format(nowDateTime);
            System.out.println(str_nowDateTime);
            str_nowDateTime = str_nowDateTime.replaceAll("-", "").replaceAll(":", "");
            StringBuilder stringBuilder = new StringBuilder(str_nowDateTime);

            StringBuilder sb = stringBuilder.replace(str_nowDateTime.indexOf("."), str_nowDateTime.indexOf(".") + 1, "");

            System.out.println(sb);
            FileOutputStream output = new FileOutputStream("C:\\Users\\15871\\Desktop\\mybatis\\" + sb + ".xls");

            wb.write(output);
            output.flush();

            //关闭流
            output.close();

    }


}
